package br3akr.DeadlyDinnerParty.Client;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import br3akr.DeadlyDinnerParty.Common.UpdateClient;

public class ClientGUI extends JFrame {
	private static final long serialVersionUID = 1L;
    
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JButton submitButton;
	private JTextField inputField;
	private PrintWriter printWriter;
	private JTabbedPane infoTabs;
	private JTextPane heldItem;
	private JList<String> charactersList;
	private JList<String> inventoryList;
	private JList<String> itemsList;
	private JLabel recovering;
    
	public ClientGUI(String name) {
		super(name);
		
		//Set up the content pane.
        addComponentsToPane(this.getContentPane());
        
		//Display the window.
        this.pack();
        this.setVisible(true);
	}
	
    private void addComponentsToPane(Container pane) {
		pane.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		textArea = new JTextArea();
		scrollPane = new JScrollPane(textArea);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		textArea.setFont(new java.awt.Font("MS Sans Serif", Font.PLAIN, 12));
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		updateBorderTitle("The Deadly Dinner Party");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 2;
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		constraints.ipadx = 200;
		constraints.ipady = 300;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		pane.add(scrollPane, constraints);
	
		/* Tabs */
		JLabel tabLabel;
		/* Character Tab */
		infoTabs = new JTabbedPane();
		Container tabPane = new Container();
		tabPane.setLayout(new GridBagLayout());
		
		/* Recovering Label */
		recovering = new JLabel("Recovering!");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		recovering.setVisible(false);
		tabPane.add(recovering, constraints);
		
		/* Held Item Label */
		tabLabel = new JLabel("Held Item:");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(tabLabel, constraints);
		
		/* Held Item Item */
		heldItem = new JTextPane();
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		heldItem.setEditable(false);
		tabPane.add(heldItem, constraints);
		
		/* Contents of Container Label */
		tabLabel = new JLabel("Inventory:");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 3;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(tabLabel, constraints);
		
		inventoryList = new JList<String>();
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(inventoryList, constraints);
		
		infoTabs.addTab("Character", tabPane);
		
		/* Area Tab */
		tabPane = new Container();
		tabPane.setLayout(new GridBagLayout());
		/* Characters Label */
		tabLabel = new JLabel("Characters in the Area:");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(tabLabel, constraints);
		
		/* Character List */
		charactersList = new JList<String>();
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(charactersList, constraints);
		
		/* Items Label */
		tabLabel = new JLabel("Items in the Area:");
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(tabLabel, constraints);
		
		/* Items List */
		itemsList = new JList<String>();
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 0;
		constraints.gridy = 3;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		tabPane.add(itemsList, constraints);
		
		infoTabs.addTab("Area", tabPane);
		tabPane = new Container();
		
		/* Commands Tab */
		infoTabs.addTab("Commands", tabPane);
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 2;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		constraints.ipadx = 50;
		constraints.ipady = 50;
		constraints.anchor = GridBagConstraints.NORTHEAST;
		pane.add(infoTabs, constraints);
		
		constraints = new GridBagConstraints();
		submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionHandler());
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridx = 0;
		constraints.gridy = 2;
		pane.add(submitButton, constraints);
	
		constraints = new GridBagConstraints();
		inputField = new JTextField();
		inputField.addActionListener(new ActionHandler());
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 1;       //aligned with button 2
		constraints.gridwidth = 2;
		constraints.gridy = 2;       //third row
		pane.add(inputField, constraints);
    }
    
    public JTextArea getTextArea() {
    	return textArea;
    }
    
    public void setPrintWriter(PrintWriter pw) {
    	this.printWriter = pw;
    }
    
    private void updateBorderTitle(String title) {
    	scrollPane.setBorder(new TitledBorder(null, title, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
				javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 16), new java.awt.Color(0, 102, 102)));
    }
    
    private class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == inputField || e.getSource() == submitButton) {
				printWriter.println(inputField.getText());
				inputField.setText("");
			}
		}
    	
    }

	public void updateClient(UpdateClient fromServer) {

		String title = "The Deadly Dinner Party";
		if (fromServer.getCharName() != null) {
			title += " - " + fromServer.getCharName();
		}
		updateBorderTitle(title);
		if (!textArea.getText().trim().equals("")) {
			textArea.append("\n\n");
		}
		textArea.append(fromServer.getOutput());
		textArea.setCaretPosition(textArea.getDocument().getLength());
		heldItem.setText(fromServer.getHeldItem());

		if (fromServer.getCharactersInArea() != null) {
			String[] characters = new String[fromServer.getCharactersInArea().size()];
			int i = 0;
			for (String character : fromServer.getCharactersInArea()) {
				characters[i++] = character;
			}
			charactersList.setListData(characters);
		}
		
		if (fromServer.getInventory() != null) {
			String[] inventory = new String[fromServer.getInventory().size()];
			int i = 0;
			for (String item : fromServer.getInventory()) {
				inventory[i++] = item;
			}
			inventoryList.setListData(inventory);
		}
		
		if (fromServer.getItemsInArea() != null) {
			String[] items = new String[fromServer.getItemsInArea().size()];
			int i = 0;
			for (String item : fromServer.getItemsInArea()) {
				items[i++] = item;
			}
			itemsList.setListData(items);
		}
		
		if (fromServer.isRecovering()) {
			recovering.setVisible(true);
		} else {
			recovering.setVisible(false);
		}
	}
}
